package DBtools;

import java.util.Date;
import java.util.List;

import model.transfermodel.BeatPackage;

import com.sun.org.apache.bcel.internal.generic.NEW;

public class HeartBeatBO extends BaseDao{
	
	private String SQLQuery = "Select * from heart_beat Where imei = ?";
	private String SQLQueryByOwner = "Select * from heart_beat Where owner = ?";
	private String SQLQueryAll = "Select * from heart_beat";
	private String SQLInsert = "INSERT INTO heart_beat(phone_num, imsi, imei, wifiip, delay,localip,checksum, longtitude,lantitude,owner) VALUES (?, ?, ?, ?, ?, ? ,? ,?, ? , ?)";
	private String SQLUpdate = "UPDATE heart_beat SET phone_num = ?, imsi = ?, wifiip = ? , delay = ?, localip = ?, checksum = ?,longtitude = ?,lantitude =?,owner =? WHERE imei = ?";
	private String SQLUpdateWithoutPhoneNum = "UPDATE heart_beat SET imsi = ?, wifiip = ? , delay = ?, localip = ?, checksum = ?,longtitude = ?,lantitude =?,owner =? WHERE imei = ?";
	
	public List findByImei(String imei){
		Object[] objects = new Object[1];
		int[]    types   = new int[1];
		
		objects[0] = imei;
		types[0] = java.sql.Types.VARCHAR;
		
		return executeQuery(SQLQuery, objects, types);
	}
	
	public List findByOwner(String Owner){
		Object[] objects = new Object[1];
		int[]    types   = new int[1];
		
		objects[0] = Owner;
		types[0] = java.sql.Types.VARCHAR;
		return executeQuery(SQLQueryByOwner, objects, types);
	}
	
	public List findAll(){
		Object[] objects = new Object[0];
		int[]    types   = new int[0];
		return executeQuery(SQLQueryAll, objects, types);
	}
	
	public boolean updateHeartBeat(BeatPackage pack){
		BaseDao baseDao = new BaseDao();
		Object[] objects = new Object[1];
		int[]    types   = new int[1];
		
		objects[0] = pack.imei;
		types[0]   = java.sql.Types.VARCHAR;
		
		List list = baseDao.executeQuery(SQLQuery, objects, types);
		
		if(list.size() == 0){
			objects = new Object[10];
			types   = new int[10];
			
			objects[0] = pack.phoneNum;
			types[0] = java.sql.Types.VARCHAR;
			
			objects[1] = pack.imsi;
			types[1] = java.sql.Types.VARCHAR;
			
			objects[2] = pack.imei;
			types[2] = java.sql.Types.VARCHAR;
			
			objects[3] = pack.wifiIP;
			types[3] = java.sql.Types.VARCHAR;
			
			objects[4] = pack.delay;
			types[4] = java.sql.Types.INTEGER;
			
			objects[5] = pack.LocalIpAddress;
			types[5] = java.sql.Types.VARCHAR;
			
			objects[6] = new Date().getTime();
			types[6] = java.sql.Types.INTEGER;
			
			objects[7] = pack.longtitude;
			types[7] = java.sql.Types.DOUBLE;
			
			objects[8] = pack.latitude;
			types[8] = java.sql.Types.DOUBLE;
			
			objects[9] = pack.owner;
			types[9] = java.sql.Types.VARCHAR;
			
			baseDao.executeUpdate(SQLInsert, objects, types);
		}else{
			if(pack.phoneNum.equals("")){
				return updateHeartBeatWithoutPhoneNum(pack);
			}
			objects = new Object[10];
			types   = new int[10];
			
			objects[0] = pack.phoneNum;
			types[0] = java.sql.Types.VARCHAR;
			
			objects[1] = pack.imsi;
			types[1] = java.sql.Types.VARCHAR;
			
			objects[2] = pack.wifiIP;
			types[2] = java.sql.Types.VARCHAR;
			
			objects[3] = pack.delay;
			types[3] = java.sql.Types.INTEGER;
			
			objects[4] = pack.LocalIpAddress;
			types[4] = java.sql.Types.VARCHAR;
			
			objects[5] = new Date().getTime();
			types[5] = java.sql.Types.INTEGER;
			
			objects[6] = pack.longtitude;
			types[6] = java.sql.Types.DOUBLE;
			
			objects[7] = pack.latitude;
			types[7] = java.sql.Types.DOUBLE;
			
			objects[8] = pack.owner;
			types[8] = java.sql.Types.VARCHAR;
			
			objects[9] = pack.imei;
			types[9] = java.sql.Types.VARCHAR;
			
			baseDao.executeUpdate(SQLUpdate, objects, types);
		}
		
		
		
		return false;
	}
	
	private boolean updateHeartBeatWithoutPhoneNum(BeatPackage pack){
		Object[] objects = new Object[9];
		int[]    types   = new int[9];
		
		objects[0] = pack.imsi;
		types[0] = java.sql.Types.VARCHAR;
		
		objects[1] = pack.wifiIP;
		types[1] = java.sql.Types.VARCHAR;
		
		objects[2] = pack.delay;
		types[2] = java.sql.Types.INTEGER;
		
		objects[3] = pack.LocalIpAddress;
		types[3] = java.sql.Types.VARCHAR;
		
		objects[4] = new Date().getTime();
		types[4] = java.sql.Types.INTEGER;
		
		objects[5] = pack.longtitude;
		types[5] = java.sql.Types.DOUBLE;
		
		objects[6] = pack.latitude;
		types[6] = java.sql.Types.DOUBLE;
		
		objects[7] = pack.owner;
		types[7] = java.sql.Types.VARCHAR;
		
		objects[8] = pack.imei;
		types[8] = java.sql.Types.VARCHAR;
		
		executeUpdate(SQLUpdateWithoutPhoneNum, objects, types);
		return false;
	}
	
	public static void main(String[] args){
		BeatPackage pack = new BeatPackage();
		
		pack.delay = 100;
		pack.imei="imei";
		pack.imsi="imsi+";
		pack.phoneNum="123456";
		pack.wifiIP="0.0.0.0";
		
		HeartBeatBO bo = new HeartBeatBO();
		for(int i=0;i<100;i++){
			pack.imei = "imei+"+i;
			bo.updateHeartBeat(pack);
		}
	}
	
}
